package com.foxzzz.array;


public class Solution643 {
    public double findMaxAverage(int[] nums, int k) {
        //1 <= k <= n <= 30,000。
        //所给数据范围 [-10,000，10,000]
        double num = 0;
        double max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            num += nums[i];
            if (i < k - 1) {
                continue;
            }

            if (i >= k) {
                num -= nums[i - k];
            }
            max = Math.max(num, max);
        }
        return max / k;
    }

    public static void main(String[] args) {
        System.out.println(new Solution643().findMaxAverage(new int[]{-1}, 1));
        System.out.println(new Solution643().findMaxAverage(new int[]{1, 12, -5, -6, 50, 3}, 4));

    }
}
